package models;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class EvaluatitionInformation
{
    // entities
    public String        source               = "";
    public String        target               = "";
    public String        sourceEntity         = "";
    public String        targetEntity         = "";

    // candidates
    public int           mcBeforeReducing     = 0;
    public int           mcAfterReducing      = 0;

    public int           change1              = 0;

    // graph
    public int           prematchedProperties = 0;
    public int           comparableProperties = 0;
    public List<Float>   propertyConf         = new ArrayList<>();

    public int           change2              = 0;
    public String        result               = "";

    private static float notSimThreshold      = 0.2f;


    // source;target;source-entity;target-entity;mc_before;mc_after;change1;premachted prop;comparable prop;not sim prop;averag prop sim;change2;result;

    private float averagePropertyConf()
    {
        if(propertyConf.isEmpty()) {
            return 0;
        } else {
            Float sum = 0f;
            int count = 0;
            for(Float f : propertyConf) {
                if(f > notSimThreshold) {
                    sum += f;
                    count += 1;
                }
                // System.out.println("EI-average: " + f + "\t" + sum + "\t" + count);
            }
            return count != 0 ? sum / (float) count : 0;
        }
    }


    private int notSimilarPropertiesCount()
    {
        int z = 0;
        for(Float f : propertyConf) {
            if(f < notSimThreshold) {
                z += 1;
            }
        }
        return z;
    }


    public void saveToFile()
    {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("data/bernd_source_dbp.csv", true));
            StringBuffer buffer = new StringBuffer();
            buffer.append(source + ";");
            buffer.append(target + ";");
            buffer.append(sourceEntity + ";");
            buffer.append(targetEntity + ";");
            buffer.append(mcBeforeReducing + ";");
            buffer.append(mcAfterReducing + ";");
            buffer.append(change1 + ";");
            buffer.append(prematchedProperties + ";");
            buffer.append(comparableProperties + ";");
            buffer.append(notSimilarPropertiesCount() + ";");
            buffer.append(averagePropertyConf() + ";");
            buffer.append(change2 + ";");
            buffer.append(result + ";");
            out.write(buffer + System.lineSeparator());
            out.flush();
            out.close();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}